﻿namespace Amazon.Checkout.Controllers
{
    using System;
    using System.Web.Mvc;

    using Orchard;
    using Orchard.Localization;
    using Orchard.Logging;
    using Orchard.Themes;
    using Orchard.UI.Admin;
    using Orchard.UI.Notify;
    using Orchard.Utility.Extensions;

    using Services;

    using ViewModels;

    [Admin]
    [Themed]
    public class PromotionsController : Controller
    {
        private readonly ICheckoutByAmazonService amazonService;
        private readonly IOrchardServices orchardServices;
        private readonly Localizer T;

        public PromotionsController(ICheckoutByAmazonService amazonService, IOrchardServices services)
        {
            this.amazonService = amazonService;
            this.orchardServices = services;

            this.T = NullLocalizer.Instance;
        }

        [HttpGet]
        public ActionResult List()
        {
            var model = new PromotionListViewModel { Promotions = this.amazonService.GetPromotions() };
            return View(model);
        }

        [HttpGet]
        public ActionResult Delete(int promotionId)
        {
            var promotion = this.amazonService.GetPromotion(promotionId);
            if (promotion == null)
            {
                this.orchardServices.Notifier.Error(this.T("Deleting Promotion failed: promotion does not exist."));
                return RedirectToAction("List");
            }

            this.amazonService.DeletePromotion(promotionId);

            this.orchardServices.Notifier.Information(this.T("Promotion {0} deleted.", promotion.PromotionCode));

            return RedirectToAction("List");
        }

        [HttpGet]
        public ActionResult Edit(int promotionId)
        {
            var promotion = this.amazonService.GetPromotion(promotionId);

            var viewModel = new PromotionNewUpdateViewModel
            {
                PromotionId = promotionId,
                PromotionCode = promotion.PromotionCode,
                Description = promotion.Description,
                DiscountRate = promotion.DiscountRate,
                FixedAmountDiscount = promotion.FixedAmountDiscount,
                PromotionType = promotion.PromotionType
            };

            return View("NewEdit", viewModel);
        }

        [HttpPost]
        public ActionResult Edit(PromotionNewUpdateViewModel viewModel)
        {
            if (!ModelState.IsValid)
            {
                return View("NewEdit", viewModel);
            }

            var promotion = this.amazonService.GetPromotion(viewModel.PromotionId);

            if (promotion == null)
            {
                this.orchardServices.Notifier.Error(this.T("Updating Promotion {0} failed: promotion does not exists", viewModel.PromotionCode));
                return View("NewEdit", viewModel);
            }

            this.amazonService.UpdatePromotion(viewModel.PromotionId, viewModel.PromotionCode, viewModel.Description, viewModel.PromotionType, viewModel.DiscountRate, viewModel.FixedAmountDiscount);

            return RedirectToAction("List");
        }

        [HttpGet]
        public ActionResult New()
        {
            return View("NewEdit", new PromotionNewUpdateViewModel());
        }

        [HttpPost]
        public ActionResult New(PromotionNewUpdateViewModel viewModel)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return View("NewEdit", viewModel);
                }

                var promotion = this.amazonService.GetPromotionByCode(viewModel.PromotionCode);
                if (promotion != null)
                {
                    this.orchardServices.Notifier.Error(this.T("Creating Promotion {0} failed: a Promotion with same code already exists", viewModel.PromotionCode));
                    return View("NewEdit", viewModel);
                }

                this.amazonService.CreatePromotion(viewModel.PromotionCode, viewModel.Description, viewModel.PromotionType, viewModel.DiscountRate, viewModel.FixedAmountDiscount);
                this.orchardServices.Notifier.Information(this.T("Promotion created"));
                return RedirectToAction("List");
            }
            catch (Exception exception)
            {
                this.Error(exception, this.T("Creating Promotion failed: {0}", exception.Message), NullLogger.Instance, this.orchardServices.Notifier);

                return View("NewEdit", viewModel);
            }
        }
    }
}